// pages/store/prod/list/list.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CustomBar: getApp().globalData.CustomBar,
    scrollHeight: '100vh',
    keyWord: '',
    items: [],
    prodTypeList:[],
    currProdTypeIndex: 0,
    currProdTypeId: 0,
    currProdTypeName: '',
    currProdList: [],
    prodMap: new Map(),
  },
  // 搜索确认
  onSearch(event) {
    this.setData({ keyWord: event.detail });
    this.qryProdList();
  },
  // 搜索清空
  onSearchClear() {
    this.setData({ keyWord: "" });
    this.qryProdList();
  },
  qryProdList() {
    const param = {
      keyWord: this.data.keyWord
    }
    getApp().callContainer('/api/store/prod/list', param, (res) => {
      const prodMap = new Map();
      if (res.data.data.prodList && res.data.data.prodList.length > 0) {
        for (const prod of res.data.data.prodList) {
          let tmpProdList = prodMap.get(prod.prodTypeId);
          if (tmpProdList && tmpProdList.length > 0) {
            tmpProdList.push(prod);
          } else {
            tmpProdList = [prod];
          }
          prodMap.set(prod.prodTypeId, tmpProdList);
        }
      }
      this.setData({ prodMap: prodMap });
      if (this.data.currProdTypeId) {
        const currProdList = prodMap.get(this.data.currProdTypeId);
        this.setData({ currProdList: currProdList || [] });
      }
      this.loadTmpFile();
    });
  },
  loadTmpFile() {
    for (const key of this.data.prodMap.keys()) {
      const currProdList = this.data.prodMap.get(key);
      for (const prod of currProdList) {
        if (prod.imgUrlList) {
          getApp().getTempFilePath(prod.imgUrlList).then((res) => {
            prod.tmpImgUrlList = res;
            this.setData({ prodMap: this.data.prodMap });
          })
        }
      }
    }
    for (const prod of this.data.currProdList) {
      if (prod.imgUrlList && !prod.tmpImgUrlList) {
        getApp().getTempFilePath(prod.imgUrlList).then((res) => {
          prod.tmpImgUrlList = res;
          this.setData({ currProdList: this.data.currProdList });
        })
      }
    }
  },
  qryProdTypeList() {
    const param = {}
    getApp().callContainer('/api/store/prodType/list', param, (res) => {
      const items = [];
      if (res.data.data.prodTypeList && res.data.data.prodTypeList.length > 0) {
        for (const data of res.data.data) {
          items.push({
            goodsTypeId: data.goodsTypeId,
            goodsTypeName: data.goodsTypeName,
            text: data.goodsTypeName,
            badge: data.goodsCount
          });
        }
        if (!this.data.currProdTypeId) {
          // 当前分类序号为空时，默认取第一个分类
          this.setData({
            currProdTypeIndex: 0,
            currProdTypeId: res.data.data[0].prodTypeId,
            currProdTypeName: res.data.data[0].name
          });
        } else {
          // 当前分类序号不为空时，用当前分类序号更新currProdTypeIndex
          let currProdTypeIndex = 0;
          for (const item of items) {
            if (item.prodTypeId == this.data.currProdTypeId) {
              break;
            }
            currProdTypeIndex++;
          }
          this.setData({ currProdTypeIndex: currProdTypeIndex });
        }
        // 更新当前分类对应的商品
        if (this.data.prodMap.size) {
          const currProdList = this.data.prodMap.get(this.data.currProdTypeId);
          this.setData({ currProdList: currProdList || [] });
        }
      }
      this.setData({
        prodTypeList: res.data.data,
        items: items,
      });
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.qryProdTypeList();
    this.qryProdList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})